<?php
require_once 'config.php';

//获取前端数据
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];

//写下查询指令
$chaxun = "SELECT * FROM list1 WHERE username = '$username'";
//执行查询指令,返回的是一个结果集
$result = mysqli_query($conn,$chaxun);
//将结果集整理成对象
$data = mysqli_fetch_assoc(($result));

if($data){
    echo json_encode(array(
        'code' => 0,
        'msg' => '用户名已存在'
    ));
}else{
    //写下插入指令
    $charu = "INSERT INTO list1 (username,password) VALUES ('$username','$password')";
    //直接执行插入指令
    mysqli_query($conn,$charu);
    //获取数据库受影响的行数
    $rows = mysqli_affected_rows($conn);
    //根据受影响行数来判断是否插入成功
    if($rows > 0 ){
        echo json_encode(array(
            'code' => 1 ,
            'msg' => '注册成功'
        ));
    }
}